#include"QuestionBox.h"
#include "Coin.h"
#include "Factory.h"
#include "Star.h"
QuestionBox::QuestionBox(D3DXVECTOR2 position,EState state) :GameObject(position , EObjectType::EQuestionBox,state)
{
	realType = EQuestionBox;
	if(m_state!=EState::Blocked)
		m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::QuestionBox_ID),3,1,3,1,2);
	else
		m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::QuestionBox_ID),3,1,3,0,0);
	item = new Coin(position,Alive);
	item->GetSprite()->m_TimeAni = 200;
	isBlocked = false;
	timer = 0;
	canUpdateItem = true;
}

QuestionBox::QuestionBox(D3DXVECTOR2 position, EObjectType itemType,EState state) :GameObject(position,EObjectType::EQuestionBox,state)
{
	switch(itemType)
	{
	case EMushroom_Red:
		realType = EQuestionBox_Red;
		break;
	case EMushroom_Green:
		realType = EQuestionBox_Green;
		break;
	case EStar:
		realType = EQuestionBox_Star;
		break;
	case EFlower:
		realType = EQuestionBox_Flower;
		break;
	}
	if(m_state!=EState::Blocked)
		m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::QuestionBox_ID),3,1,3,1,2);
	else
		m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::QuestionBox_ID),3,1,3,0,0);
	this->itemType = itemType;
	item = Factory::getIns()->GameObjectMaker(position,itemType);
	item->SetVelocity(D3DXVECTOR2(0,0));
	isBlocked = false;
	timer = 0;
	canUpdateItem = true;
	if(state==EState::Blocked)
	{
		m_pSprite->SetIndex(0);
	}
}

QuestionBox::~QuestionBox()
{
	GameObject::~GameObject();
}

void QuestionBox::Update(float dt)
{
	if(m_state == EState::Blocked)
		return;
	m_pSprite->Update(dt);
	if(m_state==EState::Hit)
	{
		switch(item->GetObjectType())
		{
		case ECoin:
			timer += dt;
			if(timer < 1)
			{
				item->Update(dt);
				item->SetVelocity(D3DXVECTOR2(0,10));	
			}
			else
			{
				item->SetObjectState(Die);
				m_state = EState::Blocked;
			}
			SoundManager::GetInst()->PlaySoundEffect(ES_Coin);
			break;
		case EObjectType::EMushroom_Red:
		case EObjectType::EMushroom_Green:
			if(item->GetObjectState()==EState::Alive && item->GetObjectState()!=Die)
			{
				item->Update(dt);
				SoundManager::GetInst()->PlaySoundEffect(ES_ItemUp);
				if(item->GetPosition().y < this->GetPosition().y+51)
				{
					item->SetVelocity(D3DXVECTOR2(0,2));
				}
				else
				{
					item->SetVelocity(D3DXVECTOR2(0,0));
					item->SetPosition(this->GetPosition()+D3DXVECTOR2(0,51));
					item->SetObjectState(EState::Move);
					item->SetDirection(Right);
					_creatingAfterObjects.push_back(item);
					m_state = EState::Blocked;
				}
			}
			break;
		case EObjectType::EFlower:
			if(item->GetObjectState()==Alive && item->GetObjectState()!=Die)
			{
				SoundManager::GetInst()->PlaySoundEffect(ES_ItemUp);
				item->Update(dt);
				if(item->GetPosition().y < this->GetPosition().y+50)
				{
					item->SetVelocity(D3DXVECTOR2(0,2));
				}
				else
				{
					item->SetVelocity(D3DXVECTOR2(0,0));
					item->SetPosition(this->GetPosition()+D3DXVECTOR2(0,50));
					item->SetObjectState(EState::Stand);
					_creatingAfterObjects.push_back(item);
					m_state = EState::Blocked;
				}
			}
			break;
		case EObjectType::EStar:
			if(item->GetObjectState()==EState::Alive && item->GetObjectState()!=Die)
			{
				item->Update(dt);
				SoundManager::GetInst()->PlaySoundEffect(ES_ItemUp);
				if(item->GetPosition().y < this->GetPosition().y+51)
				{
					item->SetVelocity(D3DXVECTOR2(0,2));
				}
				else
				{
					item->SetVelocity(D3DXVECTOR2(0,0));
					item->SetPosition(this->GetPosition()+D3DXVECTOR2(0,51));
					Star* tmpStar = (Star*)item;
					tmpStar->Jump();
					item->SetDirection(Right);
					_creatingAfterObjects.push_back(item);
					m_state = EState::Blocked;
				}
			}
			break;
		}
		m_pSprite->SetIndex(0);
	}
}


void QuestionBox::Draw()
{
	if(m_state==EState::Hit)
	{
		if(item->GetObjectState()!=Die)
			item->Draw();
	}
	m_pSprite->Draw(m_position,m_effect,0,1);
}

